package com.shop.service.impl;

import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.shop.entity.*;
import com.shop.mapper.ShopOrderMapper;
import com.shop.service.ProductProxyService;
import com.shop.service.ShopOrderProductService;
import com.shop.service.ShopOrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 订单 服务层实现。
 *
 * @author 彭新才
 * @since 2024-10-14
 */
@Service
public class ShopOrderServiceImpl extends ServiceImpl<ShopOrderMapper, ShopOrder> implements ShopOrderService {

    @Autowired
    private ProductProxyService productProxyService;
    @Autowired
    private ShopOrderProductService orderProductService;

    @Override
    public boolean add(ShopOrderBo orderBo) {
        ShopOrderProduct orderProduct = orderBo.getOrderProduct();

        Product product = productProxyService.getById(orderProduct.getProductId());
        if (product == null) {
            throw new RuntimeException("商品不存在");
        }
        ProductTemplate template = productProxyService.getTemplateById(orderProduct.getTemplateId());
        if (template == null) {
            throw new RuntimeException("该商品不存在对应的规格");
        }

        ShopOrder order = new ShopOrder();
        BeanUtils.copyProperties(orderBo, order);
        save(order);

        orderProduct.setOrderId(order.getId());
        return orderProductService.save(orderProduct);
    }
}
